#!/bin/bash

apt purge -y dnsmasq
apt autoremove -y

apt -y install dnsmasq

sudo sed -i 's/^[^#].*//' /etc/dnsmasq.conf

#sudo sed -i '1i\dhcp-host=dvv01,10.126.253.14,infinite' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-host=trv01,10.126.253.13,infinite' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-host=trn01,10.126.253.12,infinite' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-host=trc01,10.126.253.11,infinite' /etc/dnsmasq.conf

#sudo sed -i '1i\dhcp-option=tag:!prz,option:dns-server,10.126.253.1' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-option=tag:prz,option:router,10.126.252.1' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-option=option:router,10.126.253.1' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-option=option:netmask,255.255.255.0' /etc/dnsmasq.conf

#sudo sed -i '1i\dhcp-range=10.126.253.100,10.126.253.199,255.255.255.0,24h' /etc/dnsmasq.conf

#sudo sed -i '1i\listen-address=10.126.253.1' /etc/dnsmasq.conf

#sudo sed -i '1i\dhcp-name-match=set:prz,prz-client' /etc/dnsmasq.conf
#sudo sed -i '1i\dhcp-name-match=set:prz,user' /etc/dnsmasq.conf

sudo sed -i '1i\resolv-file=/etc/resolv.conf' /etc/dnsmasq.conf

#sudo sed -i '1i\log-facility=/var/log/dnsmasq.log' /etc/dnsmasq.conf
#sudo sed -i '1i\log-queries' /etc/dnsmasq.conf
#sudo sed -i '1i\log-dhcp' /etc/dnsmasq.conf


# dummy0

sudo sh -c "echo '
listen-address=10.126.253.1

dhcp-name-match=set:prz,prz-client
dhcp-name-match=set:prz,user

dhcp-range=tag:prz,10.126.253.192,10.126.253.254,255.255.255.0,24h
dhcp-range=10.126.253.64,10.126.253.127,255.255.255.0,24h

dhcp-option=option:router,10.126.253.1
dhcp-option=option:netmask,255.255.255.0
dhcp-option=tag:prz,option:dns-server,10.126.253.1
dhcp-option=option:dns-server,10.126.253.1

dhcp-host=trc01,10.126.253.11,infinite
dhcp-host=trn01,10.126.253.12,infinite
dhcp-host=trv01,10.126.253.13,infinite
dhcp-host=dvv01,10.126.253.14,infinite
dhcp-host=dvn01,10.126.253.15,infinite

' > /etc/dnsmasq.d/dummy0.conf"

#service dnsmasq reload


# Leases reset to keep permanent IPs

sudo sh -c "echo '#! /bin/bash

service dnsmasq stop
rm /var/lib/misc/dnsmasq.leases
touch /var/lib/misc/dnsmasq.leases
service dnsmasq start

' > /usr/local/bin/dnsmasq-leases-reset.sh"

sudo chmod a+x /usr/local/bin/dnsmasq-leases-reset.sh

sudo sh -c "echo '#! /bin/bash

HOUR=\`eval date +%H\`

#if [ \"\$HOUR\" = \"05\" ]; then
	sh /usr/local/bin/dnsmasq-leases-reset.sh
#fi

' > /etc/cron.hourly/dnsmasq-leases-reset"

sudo chmod a+x /etc/cron.hourly/dnsmasq-leases-reset


sh /usr/local/bin/dnsmasq-leases-reset.sh
